package com.vhall.component.service.pay.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.pay.PaymentSettingMapper;
import com.vhall.component.entity.pay.entity.PaymentSettingEntity;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.pay.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置项查询
 *
 * @author xuyongzheng
 * @since 2020/10/22
 **/
@Slf4j
@Service
public class SettingServiceImpl implements SettingService {


    @Autowired
    private PaymentSettingMapper paymentSettingMapper;


    /**
     * 查询配置列表
     *
     * @param appId
     * @param channel
     * @param serviceCode
     * @return
     */
    @Override
    public Map<String, String> mapBy(String appId, String channel, String serviceCode) {

        LambdaQueryWrapper<PaymentSettingEntity> where = Wrappers.lambdaQuery();
        List<PaymentSettingEntity> settings = paymentSettingMapper.selectList(where
                .eq(PaymentSettingEntity::getAppId, appId)
                .eq(PaymentSettingEntity::getChannel, channel)
                .eq(PaymentSettingEntity::getServiceCode, serviceCode)
                .isNull(PaymentSettingEntity::getDeletedAt));
        if (settings.size() == 0) {
            log.error("select app setting fail,app_id:[{}],channel:[{}],service_code:[{}]", appId, channel, serviceCode);
            throw new BusinessException(BizErrorCode.COMP_PAY_PARAM_UNSET);
        }

        Map<String, String> kv = new HashMap<>(16);

        for (PaymentSettingEntity entity : settings) {
            kv.put(entity.getKey(), entity.getValue());
        }
        return kv;
    }


}
